package com.example.baiduai.utils;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;


@Component
public class EhcacheUtils {

    private final static String IP ="http://222.85.139.80:41001";
    private final static String URL ="/business/abnormal/add";


    private static EhcacheUtils ehcacheUtils;
    @PostConstruct
    public void init() {
        ehcacheUtils = this;
    }


    @Autowired
    EhCacheCacheManager cacheManager;

    /**
     * 添加缓存
     */
    public static void put(String cacheName, String key, Object value){
        ehcacheUtils.cacheManager.getCache(cacheName).put(key, value);
    }

    /**
     * 查询缓存对象
     */
    public static <T> T get(String cacheName, String key, Class<T> t){
        return ehcacheUtils.cacheManager.getCache(cacheName).get(key, t);
    }

    /**
     * 查询缓存列表
     */
    public static <T> List<T> getList(String cacheName, String key){
        return ehcacheUtils.cacheManager.getCache(cacheName).get(key, List.class);
    }

    /**
     * 删除缓存
     */
    public static void delete(String cacheName, String key){
        ehcacheUtils.cacheManager.getCacheManager().removeCache(cacheName);
    }

    /**
     * 清空缓存
     * @param cacheName
     */
    public static void clear(String cacheName) {
        ehcacheUtils.cacheManager.getCache(cacheName).clear();

    }


}
